from app import db, jsonify
from marshmallow import Schema, fields
import datetime, time
from settings import FlaskSetting
from src.common.code import Code, make_result
from src.common.hashliber import md5
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, SignatureExpired, BadSignature


# ORM创建数据库存表

class Student(db.Model):
    __tablename__ = 'student'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), )
    sex = db.Column(db.Integer, )
    age = db.Column(db.Integer, )
    grade = db.Column(db.Integer)
    source = db.Column(db.Integer)
    face = db.Column(db.String(500))
    is_del = db.Column(db.Integer, default=0)
    c_time = db.Column(db.Integer, default=int(time.time() * 1000))

    def __init__(self, name, sex, age, grade, source, face):
        self.name = name
        self.sex = sex
        self.age = age
        self.grade = grade
        self.source = source
        self.face = face

    def __repr__(self):
        return f'{self.name} is face {self.face} and time {self.c_time}'

    def to_json(self):
        return {
            'id': self.id,
            'name': self.name,
            'sex': self.sex,
            'age': self.age,
            'grade': self.grade,
            'source': self.source,
            'face': self.face,
            'c_time': self.c_time,
            'is_del': self.is_del
        }


class User(db.Model):
    __user__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    user_name = db.Column(db.String(50), unique=True)
    pass_word = db.Column(db.String(200))
    nick_name = db.Column(db.String(100))
    mobile = db.Column(db.Integer)
    c_time = db.Column(db.Integer, default=int(time.time() * 1000))

    def __int__(self, id, user_name, pass_word, nick_name, mobile):
        self.id = id
        self.user_name = user_name
        self.pass_word = pass_word
        self.nick_name = nick_name
        self.mobile = mobile

    def __repr__(self):
        return f'{self.nick_name} is password {self.pass_word}'

    def generate_auth_token(self, expiration=604800):
        '''
        设置token过期时间为一周。
        :param expiration:
        :return:
        '''
        s = Serializer(secret_key=FlaskSetting.SECRET_KEY, expires_in=expiration)
        token = s.dumps(
            {
                'id': self.id
            }
        ).decode('ascii')
        return token

    @staticmethod
    def check_auth_token(token):
        s = Serializer(FlaskSetting.SECRET_KEY)
        try:
            data = s.loads(token)
        except SignatureExpired:
            return None  # valid token, but expired
        except BadSignature:
            return None  # invalid token
        return data['id']

    def to_json(self):
        return {
            'id': self.id,
            'user_name': self.user_name,
            'nick_name': self.nick_name,
            'mobile': self.mobile,
            'c_time': self.c_time,
            'token': self.generate_auth_token()
        }
